package cn.wen233.client.net;

import cn.wen233.basic.exceptions.BaseException;
import cn.wen233.basic.model.Instance;
import cn.wen233.basic.model.Namespace;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Rest 风格
 *
 * @author wenei
 * @date 2021-10-25 19:53
 */
public class RestClient {

    private static final ObjectMapper jsonMapper = new ObjectMapper();

    private static final Logger LOGGER = LoggerFactory.getLogger(RestClient.class);

    public static <T> T requestForObject(String url, List<String> headers, Map<String, String> paramValues, String body, String encoding, String method, Class<T> tClass) {
        HttpClient.HttpResult result = HttpClient.request(url, headers, paramValues, body, encoding, method);
        LOGGER.debug("http url={} method={} 请求结果:{}", url, method, result);
        if (result.getCode() != 200) {
            LOGGER.warn("请求状态码: {}", result.getCode());
            throw new BaseException("请求状态码：%s", result.getCode());
        }
        JsonNode dataNode = null;
        try {
            JsonNode resultNode = jsonMapper.readValue(result.getContent(), JsonNode.class);
            dataNode = resultNode.path("data");
        } catch (IOException e) {
            LOGGER.warn("获取rest数据格式有误 ResponseContent={}", result.getContent());
        }
        return jsonMapper.convertValue(dataNode, tClass);
    }

    public static <T> List<T> requestForList(String url, List<String> headers, Map<String, String> paramValues, String body, String encoding, String method, Class<T> tClass) {
        HttpClient.HttpResult result = HttpClient.request(url, headers, paramValues, body, encoding, method);
        if (result.getCode() != 200) {
            LOGGER.warn("请求状态码: {}", result.getCode());
            throw new BaseException("请求状态码：%s", result.getCode());
        }
        JsonNode arrayNode = null;
        try {
            JsonNode resultNode = jsonMapper.readValue(result.getContent(), JsonNode.class);
            arrayNode = resultNode.path("data");
        } catch (IOException e) {
            LOGGER.warn("获取rest数据格式有误 ResponseContent={}", result.getContent());
        }
        List<T> list = new ArrayList<>();
        if (arrayNode.getNodeType().equals(JsonNodeType.ARRAY)) {
            arrayNode.forEach(x -> list.add(jsonMapper.convertValue(x, tClass)));
        }
        return list;
    }

    public static void main(String[] args) {
        System.out.println(requestForObject("http://localhost:11112/api/services/public", null, null, null, "UTF-8",  HttpMethod.GET, Object.class));
        List<Namespace> namespaces = requestForList("http://localhost:11112/api/namespaces", null, null, null, "UTF-8", HttpMethod.GET, Namespace.class);
        System.out.println(namespaces);
    }
}
